package controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;

import dataStructure.LinkedList;

import entity.BookingRecord;
import entity.RedemptionRecord;
import entity.Staff;

public class RecordController extends ProgramFlowControl {
	private LinkedList<BookingRecord> bookingRecords;
	private LinkedList<RedemptionRecord> redemptionRecords;

	public RecordController(LinkedList<BookingRecord> bookingRecords, LinkedList<RedemptionRecord> redemptionRecords, Staff staff) throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
		super("Record Checking");

		this.bookingRecords = bookingRecords;
		this.setRedemptionRecords(redemptionRecords);

		this.addNewChoice("1", "1. View Booking Record", this, "searchBookingRecordAction");
		this.addNewChoice("2", "2. View Redemption Record", this, "searchRedemptionRecordAction");
		this.setTeminateOption("x", "Press x to return to Main Menu", this, "clearUp");
	}

	public void begin() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
		this.flowControl();
	}

	public void searchBookingRecordAction() throws IOException, SecurityException, IllegalArgumentException, NullPointerException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException {
		String recordID = UserInputControl.getStrInput("Booking Invoice ID:", "[0-9]{6}");
		BookingRecord result = this.searchBookingRecord(recordID);
		if (result != null)
			result.printContent();
		else
			System.out.println("Record [ID: " + recordID + "] not found");
	}

	public BookingRecord searchBookingRecord(String recordID) throws SecurityException, IllegalArgumentException, NullPointerException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException {
		BookingRecord result = this.bookingRecords.searchContent("recordID", recordID);

		if (result != null)
			return result;
		else
			return null;
	}

	public void searchRedemptionRecordAction() throws IOException, SecurityException, IllegalArgumentException, NullPointerException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException {
		String recordID = UserInputControl.getStrInput("Redemption Receipt ID:", "[0-9]{6}");
		RedemptionRecord result = this.searchRedemptionRecord(recordID);
		if (result != null)
			result.printContent();
		else
			System.out.println("Receipt [ID: " + recordID + "] not found");
	}

	public RedemptionRecord searchRedemptionRecord(String recordID) throws SecurityException, IllegalArgumentException, NullPointerException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ParseException {
		RedemptionRecord result = this.redemptionRecords.searchContent("recordID", recordID);

		if (result != null)
			return result;
		else
			return null;
	}

	public void clearUp() {
	}

	public LinkedList<RedemptionRecord> getRedemptionRecords() {
		return redemptionRecords;
	}

	public void setRedemptionRecords(LinkedList<RedemptionRecord> redemptionRecords) {
		this.redemptionRecords = redemptionRecords;
	}
}
